package com.example.whm;



import java.util.ArrayList;
import java.util.List;

import org.holoeverywhere.LayoutInflater;
import org.holoeverywhere.app.Application;
import org.holoeverywhere.app.Fragment;
import org.holoeverywhere.preference.SharedPreferences;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.androidquery.AQuery;
import com.example.whm.lib.client;
import com.example.whm.model.DateRange;
import com.viewpagerindicator.TabPageIndicator;
 
public class ThreeFragment extends Fragment {

	private AQuery aq;
	private AQuery aq2;
	String result;
	private String topic;
	View view ;
	ListView listview;
	List<DateRange> data=new ArrayList<DateRange>();
	ViewPager pager;
	TabPageIndicator indicator ;
	PageAdapter adapter;
	FragmentManager fm;
    @Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
    	 view = inflater.inflate(R.layout.three_layout, container, false);
    		aq = new AQuery(getActivity(), view);
        	aq2 = new AQuery(getActivity());
        	//aq.find(R.id.three_layout_progress).visible();
        	pager = (ViewPager) view.findViewById(R.id.three_layout_pager);	   
        	indicator = (TabPageIndicator)view.findViewById(R.id.three_layout_indicator);
        	SharedPreferences sp=((Application)getActivity().getApplication()).getSharedPreferences("whm",Context.MODE_PRIVATE);
        	
        	data=client.pre_cache_get(sp, DateRange.class, "ids");
        	System.out.println("ThreeFragment ids=================="+data.size());
        	
        	
        	
        	
    	// listview=(ListView) view.findViewById(R.id.three_layout_listview);
    	return view;
	}




	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		
	 fm=getChildFragmentManager();
	   adapter = new PageAdapter(fm,data);
	    pager.setAdapter(adapter);
	    pager.setOffscreenPageLimit(0);
	    indicator.setViewPager(pager);
	    
	//	init();
		
	}
	
	
	
	

/*	public void init(){
		
		
		String url = "http://"+Whm_app.api_url+":9000/api/list_subcribe";
    	//String url = "http://219.239.236.77/statuses/puzzles";
      	aq.ajax(url, String.class, 0, this, "parsedata");
	}
	
    public void parsedata(String url, String s, AjaxStatus status){
    	System.out.println("status.getCode()==="+status.getCode());
    	if (status.getCode()==200) {
    		if(s != null){
        		ui(s);
        	}
		}else{
			//aq.find(R.id.three_layout_progress).gone();
		}
    	
    	
    }

 public void ui(String str){
	 final String s=str;
	 new Thread( new Runnable() {
         public void run() {
        	// String s1= JSON.parseObject(s).getString("users");
        	 result=s;
        	 String s1= JSON.parseObject(s).getString("result");
        	 String s2= JSON.parseObject(s1).getString("ids");
        	 
     		System.out.println(s2);
     		
     		 data=JSON.parseArray(s2, DateRange.class);

        	 System.out.println(data.size());
                 getActivity().runOnUiThread(new Runnable() {
					
					@Override
					public void run() {
					//	Toast.makeText(getActivity(), "list.size="+list.size(), 1000).show(); 
			     	//	Toast.makeText(getActivity(), "data.size="+data.size(), 1000).show(); 
						
						
						    
						    indicator = (TabPageIndicator)view.findViewById(R.id.three_layout_indicator);
						    indicator.setViewPager(pager);  
						
						aq.id(R.id.three_layout_listview).adapter(new adapter(data)).visible();
						//aq.id(R.id.three_layout_listview).visible();
						aq.find(R.id.three_layout_progress).gone();
					}
				});
          }
 }).start();
 } */
	


//public static String[] topics = {"dog","bird", "panda", "horse","cat", "bird", "panda", "horse", "elephant", "bear", "butterfly", "monkey", "fish", "tiger", "chicken", "pig"};
	
	public  class PageAdapter extends FragmentStatePagerAdapter{

		List<DateRange> data;
		public PageAdapter(FragmentManager fm,List<DateRange> da) {
			super(fm);
			this.data=da; 
		}

		@Override
		public Fragment getItem(int pos) {
			
			//AQUtility.debug("primary", topics[pos]);
			//TwoFragmentItem f=new TwoFragmentItem();
			Bundle args = new Bundle();
			
			args.putString("id", data.get(pos).getId());
			args.putInt("pos", pos);
			if (pos==0) {
				
				args.putString("result", result);
			}
			ThreeFragmentItem fragment = (ThreeFragmentItem) Fragment.instantiate(ThreeFragmentItem.class, args);
			//System.out.println("------------------------1"+(fragment==null));
			
			return fragment;
		}

		@Override
		public int getCount() {
			return data.size();
		}
		


		@Override
        public CharSequence getPageTitle (int pos) {
			//data.get(pos).getName()
			System.out.println("data.get(pos).getName()---------"+data.get(pos).getName());
            return data.get(pos).getName();
        }
		
		@Override
		public void setPrimaryItem(ViewGroup container, int position, Object object){
			
			//AQUtility.debug("primary", topics[position]);
	
			System.out.println("--------setPrimaryItem------------");
			if (position!=0) {
				
				ThreeFragmentItem fragment = (ThreeFragmentItem)object;
				fragment.init();
			}
			
		}
		
		@Override
		public void destroyItem(ViewGroup container, int position, Object object){
			//AQUtility.debug("destroyItem", topics[position]);
		}
		
		
	}

    
 
}
